<?php

namespace app\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\User as UserModel;

/**
 * User represents the model behind the search form about `app\models\User`.
 */
class User extends UserModel {
    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'role', 'country_id', 'material_level'], 'integer'],
            [['username', 'password', 'create_at', 'update_at'], 'safe'],
            [['start_currency', 'current_currency', 'material_inventory', 'products_inventory', 'total_loan', 'previous_netasset', 'current_netasset', 'netasset_growth', 'tech_level'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios() {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params) {
        $query = UserModel::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'role' => $this->role,
            'country_id' => $this->country_id,
            'start_currency' => $this->start_currency,
            'current_currency' => $this->current_currency,
            'material_inventory' => $this->material_inventory,
            'products_inventory' => $this->products_inventory,
            'total_loan' => $this->total_loan,
            'previous_netasset' => $this->previous_netasset,
            'current_netasset' => $this->current_netasset,
            'netasset_growth' => $this->netasset_growth,
            'material_level' => $this->material_level,
            'tech_level' => $this->tech_level,
            'create_at' => $this->create_at,
            'update_at' => $this->update_at,
        ]);

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'password', $this->password]);

        return $dataProvider;
    }
}
